package com.example.yiparkapplication.activity.index.activity.ConvenientPaymentActivity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yiparkapplication.R;
import com.example.yiparkapplication.entity.SingleCarSQL;

import java.text.SimpleDateFormat;
import java.util.List;

public class CarOrderAdapter extends RecyclerView.Adapter<CarOrderAdapter.ViewHolder> {
    private List<SingleCarSQL> mSingleCarSQLList;
    private CarOrderNopayActivity mCarorderNopayActivity;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static class ViewHolder extends RecyclerView.ViewHolder{
        View carOrderView;
        TextView routeLocationName;
        TextView startTime;
        TextView endTime;
        TextView price;
        TextView truckSpace ;
        TextView licensePlate;
        TextView singleCarType;
        TextView singleCarCouponCard;
        public ViewHolder(View view){
            super(view);
            carOrderView = view;
            routeLocationName = view.findViewById(R.id.id_street_name);
            startTime = view.findViewById(R.id.id_single_car_start_time);
            endTime = view.findViewById(R.id.id_single_car_end_time);
            price = view.findViewById(R.id.id_single_car_price);
            truckSpace = view.findViewById(R.id.id_single_car_truck_space);
            licensePlate = view.findViewById(R.id.id_single_car_license_plate);
            singleCarType = view.findViewById(R.id.id_single_car_type);
            singleCarCouponCard = view.findViewById(R.id.id_single_car_coupon_card);
        }
    }

    public CarOrderAdapter(List<SingleCarSQL> singleCarSQL, CarOrderNopayActivity carOrderNopayActivity){
        mSingleCarSQLList = singleCarSQL;
        mCarorderNopayActivity = carOrderNopayActivity;
    }

    @Override
    public CarOrderAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_car_order_layout, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.carOrderView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getAdapterPosition();
                SingleCarSQL singleCarSQL = mSingleCarSQLList.get(position);
                String singleCarID = singleCarSQL.getSingleCarId().toString();
                if (mCarorderNopayActivity != null){
                    mCarorderNopayActivity.toCarOrderActivity(singleCarID);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position){
        SingleCarSQL singleCarSQL = mSingleCarSQLList.get(position);

        holder.routeLocationName.setText(singleCarSQL.getRouteLocationName());
        holder.startTime.setText(simpleDateFormat.format(singleCarSQL.getSingleCarStartTime()));
        holder.endTime.setText(simpleDateFormat.format(singleCarSQL.getSingleCarEndTime()));
        String mPrice = "￥" + singleCarSQL.getSingleCarPrice();
        holder.price.setText(mPrice);
        holder.truckSpace.setText(singleCarSQL.getSingleCarTruckSpace());
        holder.licensePlate.setText(singleCarSQL.getSingleCarLicensePlate());
        if (singleCarSQL.getSingleCarType() == 1 || singleCarSQL.getSingleCarPaid() == 0){
            holder.singleCarType.setText("待支付");
            if (singleCarSQL.getSingleCarPaid() > 0 && singleCarSQL.getSingleCarPaid() < singleCarSQL.getSingleCarPrice()){
                holder.singleCarType.setText("翼支付" + singleCarSQL.getSingleCarPaid() + "元");
            }
        }else if (singleCarSQL.getSingleCarType() == 2 || singleCarSQL.getSingleCarType() == 3){
            if (singleCarSQL.getSingleCarPaid().equals(singleCarSQL.getSingleCarPrice())){
                holder.singleCarType.setText("已完成");
            }else{
                holder.singleCarType.setText("翼支付" + singleCarSQL.getSingleCarPaid() + "元");
            }
        }
        if (singleCarSQL.getDiscountCardId() != null && singleCarSQL.getDiscountCardId() != 0){
            holder.singleCarCouponCard.setText("优惠减免");
        }else if (singleCarSQL.getCouponId() != null && singleCarSQL.getDiscountCardId() != 0){
            holder.singleCarCouponCard.setText("优惠减免");
        }
    }

    @Override
    public int getItemCount(){
        return mSingleCarSQLList.size();
    }
}
